<?php
class C_suara extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_suara');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'INSERT' :
				$this->_insert();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			default :
				$this->load->view('app/v_suara');
		}
	}
	
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['suara_text'] = nosql_post("suara_text");
		$data['suara_file'] = nosql_post("suara_file");
		echo $this->m_suara->_list($data);
	}
	
	function _insert(){
        $suara_file =  $this->_upload();
		$data = array(
			'suara_text' => $this->input->post('suara_text'), 
			'suara_file' => $suara_file, 
			  
			'suara_active'=> '1',
			'suara_insert_user'	=>$_SESSION[SESSION_USERID],
			'suara_insert_date'=> date("Y-m-d h:i:s"));
		$this->m_suara->_insert($data); 
		echo 1;
	}
	
    function _upload(){
        $dir = "assets/audio/";
        move_uploaded_file( $_FILES["suara_file"]["tmp_name"],$dir.$_FILES["suara_file"]["name"]);
        return $_FILES["suara_file"]["name"];
    }
    
	function _update(){
        $suara_file =  $this->_upload();
		$suara_id = $this->input->post('suara_id');
		$data = array(
			'suara_text' => $this->input->post('suara_text'), 
			'suara_file' => $suara_file, 
			
			'suara_update_user'	=> $_SESSION[SESSION_USERID],
			'suara_update_date'=> date("Y-m-d h:i:s"));
		$this->m_suara->_update($data,$suara_id);
		echo 1;
 	}
	
	function _delete(){
		$suara_id= $this->input->post('suara_id');
		$data = array(
			'suara_active' => '0'
		);
		$this->m_suara->_delete($data,$suara_id);
		echo 1;
	}
}